
from utils.StandardSqlite3DB import SQLiteDB
from mytools import calculate_time

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np


"""
db_table20250508_151641
db_table20250508_162035
db_table20250508_163138
db_table20250508_172004
db_table20250508_224707
db_table20250508_230712
db_table20250508_231318
db_table20250509_233343
db_table20250509_233849
db_table20250510_235028
db_table20250510_235412
db_table20250511_232304
"""

table = 'db_table20250511_232304'
date = '2025-05-12'
db = SQLiteDB('fenbei_monitor.db')

# 设置默认模板
import plotly.io as pio
pio.templates.default = "seaborn"  # 可选: plotly, plotly_white, plotly_dark, ggplot2, seaborn

#折线
@calculate_time
def a2(x,y):
    fig = px.line(x=x,
                  y=y,
                  title="db数据",
                  labels={"x": "time", "y": "db"})
    #60db 水平参考线
    fig.add_hline(y=60,
                  line_dash="dash",#line_dash: 线型（"solid", "dot", "dash", "longdash", "dashdot", "longdashdot"）
                  line_color="red",
                  line_width=3)
    # 添加滚动条
    # 添加范围选择器
    fig.update_xaxes(
        rangeslider_visible=True,  # 显示底部滑条
        rangeselector=dict(
            buttons=list([
                dict(
                    count=1,  # 时间跨度数值
                    label="1h",  # 按钮上显示的标签文本
                    step="hour",  # 时间单位 "second", "minute", "hour", "day", "month", "year"
                    stepmode="backward"  # 计算模式（从当前时间向前推算）  "backward"	从当前时间点向前回溯指定时间长度 "todate"	从当前时间单位的起始点至今
                ),
                dict(count=6, label="6h", step="hour", stepmode="backward"),
                #dict(count=1, label="本年至今", step="year", stepmode="todate"),

                dict(step="all", label="全部")  # 重置按钮
            ])
        )
    )

    fig.show()

#直方 err
@calculate_time
def a1(x,y):

    fig = px.bar(x=x, y=y)
    fig.show()

if __name__ == '__main__':

    data = db.fetch_all(f"SELECT * FROM {table} WHERE time>='{date} 00:30' and time<='{date} 07:00'")
    print('数据长度', len(data))
    listx = []
    listy = []
    for i in data:
        listx.append(i[1])
        listy.append(int(i[2]))
    arr_x = np.array(listx)
    arr_y = np.array(listy)
    print(arr_y)
    a2(arr_x,arr_y)


